﻿package app.view.components.sections.expo 
{
	import app.controller.StartSiteCommand;
	import app.model.RemoteProxy;
	import app.model.types.SectionType;
	import app.model.vo.SectionVO;
	import app.view.components.buttons.GlamurBtnBase;
	import app.view.components.guiDima.slideShowD.Image;
	import app.view.components.guiDima.switchContent.SwitchContentTextDBase;
	import app.view.components.header.HeadMarkerBase;
	import app.view.components.sections.LabeledSectionBase;
	import app.view.events.UIEvent;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import gs.TweenLite;
	
	/**
	 * Представиление экспозиций в виде "ленты"
	 * @author Dmitryi_Koruagin, icq:585466579, dmitryikoruagin@yandex.ru
	 */
	public class ExpoCurrentExpositionDBase extends LabeledSectionBase
	{
		
		// app.view.components.sections.expo.ExpoCurrentExpositionDBase
		
		//	public
		public var textExposition:TextField;
		public var textDate:TextField;
		public var textTitle:TextField;
		public var boxSwitch:SwitchContentTextDBase
		//	private
		public var slider:Sprite;
		public var bgdImage_1:Sprite;
		public var bgdImage_2:Sprite;
		public var bgdImage_3:Sprite;
		public var bgdImage_4:Sprite;
		public var bgdImage_5:Sprite;
		private var _slider:Image;
		private var _image1:Image;
		private var _image2:Image;
		private var _image3:Image;
		private var _image4:Image;
		private var _image5:Image;
		private var _imageArray:Array;
		//	const
		private var _currentId:int;
		private var _isLast:Boolean;
		private var _isNext:Boolean;
		private static const TIME_SHOW_NAVIGATE:Number = 0.3;
		private static const TIME_HIDE_NAVIGATE:Number = 0.2;
		
		public var narrow_left:GlamurBtnBase;
		public var currExpo_Btn:GlamurBtnBase;
		public var narrow_right:GlamurBtnBase;
		private var _arrayDataItem:Array;
		
		//	CONSTRUCTOR
		public function ExpoCurrentExpositionDBase() 
		{
			super();
		}
		
		//	METHODS
		//	private
		override protected function init():void
		{
			super.init();
			
			// для области справа (тупо лень делать по другому)
			_slider = new Image(slider.width, slider.height, Image.RESIZE_MAX, Image.ALIGN_CENTER);
			addChild(_slider);
			_slider.x = slider.x;
			_slider.y = slider.y;
			addChild(slider);
			
			_image1 = new Image(bgdImage_1.width, bgdImage_1.height, Image.RESIZE_MAX, Image.ALIGN_CENTER);
			addChild(_image1);
			_image1.x = bgdImage_1.x;
			_image1.y = bgdImage_1.y;
			addChild(bgdImage_1);
			
			_image2 = new Image(bgdImage_2.width, bgdImage_2.height, Image.RESIZE_MAX, Image.ALIGN_CENTER);
			addChild(_image2);
			_image2.x = bgdImage_2.x;
			_image2.y = bgdImage_2.y;
			addChild(bgdImage_2);
			
			_image3 = new Image(bgdImage_3.width, bgdImage_3.height, Image.RESIZE_MAX, Image.ALIGN_CENTER);
			addChild(_image3);
			_image3.x = bgdImage_3.x;
			_image3.y = bgdImage_3.y;
			addChild(bgdImage_3);
			
			_image4 = new Image(bgdImage_4.width, bgdImage_4.height, Image.RESIZE_MAX, Image.ALIGN_CENTER);
			addChild(_image4);
			_image4.x = bgdImage_4.x;
			_image4.y = bgdImage_4.y;
			addChild(bgdImage_4);
			
			_image5 = new Image(bgdImage_5.width, bgdImage_5.height, Image.RESIZE_MAX, Image.ALIGN_CENTER);
			addChild(_image5);
			_image5.x = bgdImage_5.x;
			_image5.y = bgdImage_5.y;
			addChild(bgdImage_5);
			
			textExposition.wordWrap = true;
			textExposition.multiline = true;
			
			textTitle.wordWrap = true;
			textTitle.multiline = true;
			
			_isLast = false;
			_isNext = false;
			narrow_left.alpha = 0;
			narrow_left.x = 1000;
			narrow_right.alpha = 0;
			
			currExpo_Btn.visible = false;
		}
		
		private function setEvents():void
		{
			_image1.buttonMode = _image1.useHandCursor = true;
			_image2.buttonMode = _image2.useHandCursor = true;
			_image3.buttonMode = _image3.useHandCursor = true;
			_image4.buttonMode = _image4.useHandCursor = true;
			_image5.buttonMode = _image5.useHandCursor = true;
			_image1.addEventListener(MouseEvent.CLICK, onCLickImage1);
			_image2.addEventListener(MouseEvent.CLICK, onCLickImage2);
			_image3.addEventListener(MouseEvent.CLICK, onCLickImage3);
			_image4.addEventListener(MouseEvent.CLICK, onCLickImage4);
			_image5.addEventListener(MouseEvent.CLICK, onCLickImage5);
			
			currExpo_Btn.mouseChildren = false;
			currExpo_Btn.mouseEnabled = false;
		}
		
		private function removeEvents():void
		{
			_image1.buttonMode = _image1.useHandCursor = false;
			_image2.buttonMode = _image2.useHandCursor = false;
			_image3.buttonMode = _image3.useHandCursor = false;
			_image4.buttonMode = _image4.useHandCursor = false;
			_image5.buttonMode = _image5.useHandCursor = false;
			_image1.removeEventListener(MouseEvent.CLICK, onCLickImage1);
			_image2.removeEventListener(MouseEvent.CLICK, onCLickImage2);
			_image3.removeEventListener(MouseEvent.CLICK, onCLickImage3);
			_image4.removeEventListener(MouseEvent.CLICK, onCLickImage4);
			_image5.removeEventListener(MouseEvent.CLICK, onCLickImage5);
			
			
			
		}
		
		private function setSelectedImage(ind:int=1):void
		{
			
			var obj:SectionVO = new SectionVO();
			obj.id = SectionType.POPUP_EXPO_FIVE_EXPOSITION;
			obj.data = _imageArray;
			obj.currentId = String(ind);
			obj.params = null;
			
			
			var ev:UIEvent = new UIEvent(UIEvent.POPUP_OPEN);
			ev.data = obj;
			dispatchEvent(ev);
			
		}
		
		override public function onDestroy():void 
		{
			// чистим что тут есть и вызываем супера
			removeEvents();
			
			_slider.destroy();
			removeChild(_slider);
			_image1.destroy();
			removeChild(_image1);
			_image2.destroy();
			removeChild(_image2);
			_image3.destroy();
			removeChild(_image3);
			_image4.destroy();
			removeChild(_image4);
			_image5.destroy();
			removeChild(_image5);
			
			narrow_left.destroy();
			currExpo_Btn.destroy();
			narrow_right.destroy();
			
			boxSwitch.destroy();
			
			super.onDestroy();
		};
		
		override public function get data():Object { return _data; };
		
		override public function set data(value:Object):void 
		{
			super.data = value;
			
			trace('data!!!+++', _data.params);
			
			
			var head:String = '';
			for each(var objecthead:* in _data.params.head)
			{
				head = String(objecthead.text).substr(1);
			}
			
			setParamHead(HeadMarkerBase.BGD_GREEN, head);
			
			narrow_left.setLang(_data.lang);
			currExpo_Btn.setLang(_data.lang);
			narrow_right.setLang(_data.lang);
			
			for each(var object:* in _data.params.image_text)
			{
				var exposition:Object = new Object();
				
				exposition.text = String(object.text).substr(1);
				exposition.image = (object.image[1].image_path != '') ? RemoteProxy.DOMEN+object.image[1].image_path :  '';
				
				//trace('***', object.text);
				//trace('***', object.image[1].image_path);
			}
			
			_slider.loadImage(exposition.image);
			textExposition.htmlText = exposition.text;
			
			_imageArray = new Array();
			
			for each(var objectImage:* in _data.params.img)
			{
				//trace('image+++',objectImage.image[1].image_path);
				var img:String='';
				
				img = (objectImage.image[1].image_path != '') ? RemoteProxy.DOMEN+objectImage.image[1].image_path : '';
				
				_imageArray.push(img);
			}
			
			if (_imageArray.length<5) 
			{
				for (var i:int = 0; i < 5-_imageArray.length; i++) 
				{
					_imageArray.push('');
				}
			}
			
			_image1.loadImage(_imageArray[0]);
			_image2.loadImage(_imageArray[1]);
			_image3.loadImage(_imageArray[2]);
			_image4.loadImage(_imageArray[3]);
			_image5.loadImage(_imageArray[4]);
			
			_arrayDataItem = new Array();
			
			for each(var objectExpo:* in _data.params.expo)
			{
				var objExposition:Object = new Object();
				objExposition.name = objectExpo.text[1];
				objExposition.begin = objectExpo.text[2];
				objExposition.end = objectExpo.text[3];
				
				var arrTxt:Array = new Array();
				
				for (var k:int = 4; k < 8; k++) 
				{
					if (objectExpo.text[k]!='') 
					{
						arrTxt.push( { text:objectExpo.text[k] } );
						trace('333', objectExpo.text[k]);
					}
					
					//trace('----------');
				}
				
				objExposition.text = arrTxt;
				
				
				//trace('+*', arrTxt.length);
				
				_arrayDataItem.push(objExposition);
			}
			
			
			_currentId = 0;
			
			setContentParam(_currentId);
			
			checkedNavigation();
			
			setEvents();
		};
		
		private function checkedNavigation():void
		{
			// проверяем навигацию
			// проверка
			////trace('checkedNavigation---+++',_currentId);
			if (_currentId>=1) 
			{
				TweenLite.to(narrow_left, TIME_HIDE_NAVIGATE, { alpha:1 } );
				narrow_left.addEventListener(MouseEvent.CLICK, onClickLeftBtn);
				narrow_left.buttonMode = narrow_left.useHandCursor = true;
				_isLast = true;
				//narrow_left.selected = !_isLast;
			}else
			{
				narrow_left.removeEventListener(MouseEvent.CLICK, onClickLeftBtn);
				_isLast = false;
				//narrow_left.selected = !_isLast;
				TweenLite.to(narrow_left, TIME_HIDE_NAVIGATE, { alpha:0 } );
				narrow_left.buttonMode = narrow_left.useHandCursor = false;
			}
			
			if (_currentId<_arrayDataItem.length-1) 
			{
				TweenLite.to(narrow_right, TIME_HIDE_NAVIGATE, { alpha:1 } );
				narrow_right.addEventListener(MouseEvent.CLICK, onClickRightBtn);
				_isNext = true;
				narrow_right.buttonMode = narrow_right.useHandCursor = true;
				//narrow_right.selected = !_isNext;
			}else
			{
				narrow_right.removeEventListener(MouseEvent.CLICK, onClickRightBtn);
				_isNext = false;
				//narrow_right.selected = !_isNext;
				TweenLite.to(narrow_right, TIME_HIDE_NAVIGATE, { alpha:0 } );
				narrow_right.buttonMode = narrow_right.useHandCursor = false;
			}
			
		}
		
		/**
		 * установим нужные параметры сонтенту
		 */
		private function setContentParam(n:int):void
		{
			
			if (_arrayDataItem.length>0) 
			{
				textDate.text = _arrayDataItem[n].begin + ' - ' + _arrayDataItem[n].end;
				textTitle.text = _arrayDataItem[n].name;
				
				if (_arrayDataItem[n].text.length>0) 
				{
					boxSwitch.data = _arrayDataItem[n].text;
				}
				
				//trace('set_ContentParam = ', n, _arrayDataItem[n].text.length);
			}else
			{
				textDate.text = '';
				textTitle.text = '';
				
				currExpo_Btn.visible = false;
			}
			
		}
		
		/**
		 * предыдущее событие
		 * @param	e
		 */
		private function onClickLeftBtn(e:MouseEvent):void 
		{
			_currentId = _currentId - 1;
			setContentParam(_currentId);
			checkedNavigation();
		}
		
		/**
		 * следущее события
		 * @param	e
		 */
		private function onClickRightBtn(e:MouseEvent):void 
		{
			_currentId = _currentId + 1;
			setContentParam(_currentId);
			checkedNavigation();
		}
		
		private function onCLickImage1(e:MouseEvent):void 
		{
			setSelectedImage(1);
		}
		
		private function onCLickImage2(e:MouseEvent):void 
		{
			setSelectedImage(2);
		}
		
		private function onCLickImage3(e:MouseEvent):void 
		{
			setSelectedImage(3);
		}
		
		private function onCLickImage4(e:MouseEvent):void 
		{
			setSelectedImage(4);
		}
		
		private function onCLickImage5(e:MouseEvent):void 
		{
			setSelectedImage(5);
		}
	}

}